home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual dBase Pro v7.0 / DATA1.CAB / Sample_dBASE / Mugs / Mugs.prg < prev    next >
Encoding:
Text File  |  1997-11-20  |  12.0 KB  |  443 lines

  1. //--------------------------------------------------------------
  2. //
  3. //  Mugs.prg - Mugs Sample Application
  4. //
  5. //  Main procedure for the Mugs application. This file defines
  6. //  two classes: MugApp and DataAction. MugApp contains methods
  7. //  for opening all forms and dialogs. The DataAction class
  8. //  contains data navigation and modifition methods that are 
  9. //  called from the toolbar and menu.
  10. //  
  11. //  Dependencies: < See SET PROCEDURE list at the top of the 
  12. //                  MugApp constructor. >
  13. //
  14. //  Visual dBASE Samples Group
  15. //
  16. //  $Revision:   1.24  $
  17. //
  18. //  Copyright (c) 1997, Borland International, Inc. 
  19. //  All rights reserved.
  20. //
  21. //---------------------------------------------------------------
  22.  
  23.    local app, sRunFolder
  24.    private sAppFolder
  25.    SET TALK OFF
  26.    sRunFolder = SET("DIRECTORY")
  27.    sAppFolder = SUBSTR( PROGRAM(0), 1, ;
  28.                         LEN( PROGRAM( 0 ) ) - ( LEN( PROGRAM() ) + 4 ) )
  29.  
  30.    sRunFolder := '"' + sRunFolder + '"'
  31.    sAppFolder := '"' + sAppFolder + '"'
  32.    SET DIRECTORY TO &sAppFolder.
  33.  
  34.    // If running as a compiled app, extract
  35.    // MugsR.dll if not found on disk.
  36.    if ( "runtime" $ LOWER( VERSION(0) ) )
  37.       copyOutOfEXE("MugsR.dll")
  38.    endif
  39.  
  40.    app = new MugApp( sRunFolder )
  41. return ( app.open() )
  42.  
  43. function copyOutOfEXE( sFile )
  44.    local bCopied
  45.    bCopied = false
  46.    if ( ( not FILE( sFile ) ) and FILE( sFile, true ) )
  47.       COPY FILE ( sFile ) TO ( sFile )
  48.       bCopied := true
  49.    endif
  50. return ( bCopied )
  51.  
  52. class MugApp( sRunFolder )
  53.    SET PROCEDURE TO PROGRAM(0)
  54.    SET PROCEDURE TO "mugs.mnu"           ADDITIVE
  55.    SET PROCEDURE TO "mugbars.prg"        ADDITIVE
  56.    SET PROCEDURE TO "about.wfm"          ADDITIVE
  57.    SET PROCEDURE TO "fileOpen.wfm"       ADDITIVE
  58.    SET PROCEDURE TO "customer.wfm"       ADDITIVE
  59.    SET PROCEDURE TO "inventory.wfm"      ADDITIVE
  60.    SET PROCEDURE TO "supplier.wfm"       ADDITIVE
  61.    SET PROCEDURE TO "invoice.wfm"        ADDITIVE
  62.    SET PROCEDURE TO "toolbar.wfm"        ADDITIVE
  63.    SET PROCEDURE TO "ichart.wfm"         ADDITIVE
  64.    SET PROCEDURE TO "invoiceOpen.wfm"    ADDITIVE
  65.    SET PROCEDURE TO "customerSelect.wfm" ADDITIVE
  66.    SET PROCEDURE TO "finder.wfm"         ADDITIVE
  67.    SET PROCEDURE TO "ireport.wfm"        ADDITIVE
  68.    SET PROCEDURE TO "invoice.rep"        ADDITIVE
  69.  
  70.    _app.framewin.app = this
  71.    this.restoreSet   = new RestoreSet( sRunFolder )  
  72.    this.dataAction   = new DataAction( this )
  73.    this.rootMenu     = new MugsMenu( _app.framewin, "Root")
  74.    this.toolbars     = new MugBars( this )
  75.  
  76.    function open
  77.       CLOSE FORMS
  78.       SET DESIGN OFF
  79.       shell( false, true )
  80.       _app.speedbar := false 
  81.       _app.framewin.text := "Mugs"
  82.       this.toolbars.create()
  83.       this.toolbars.appbar.attach( _app.framewin )
  84.       this.rootMenu.onInitMenu()
  85.    return( class::OpenFileOpenDialog( this ) )
  86.    
  87.    function close
  88.       local f
  89.       PRIVATE sFolder
  90.       sFolder = this.restoreSet.folder
  91.       CLOSE FORMS
  92.       SET DIRECTORY TO &sFolder.
  93.       this.toolbars.appbar.detach( _app.framewin )
  94.       if ( "runtime" $ LOWER( VERSION(0) ) )
  95.          quit
  96.       else
  97.          with ( _app )
  98.             framewin.app  := null
  99.             framewin.text := this.restoreSet.frameText
  100.             speedbar      := this.restoreSet.speedBar
  101.          endwith      
  102.          _app.framewin.root.release()    
  103.          SET DESIGN ON
  104.  
  105.          // Use temp form to force a refresh of the menubar.      
  106.          f = new Form()
  107.          f.top = -100
  108.          f.open()
  109.          f.release()
  110.  
  111.          shell( true, true )
  112.          this.toolbars.release()
  113.          with ( this )
  114.             restoreSet := null
  115.             dataAction := null
  116.             rootMenu   := null
  117.             toolbars   := null
  118.          endwith
  119.          SET PROCEDURE TO
  120.       endif
  121.    return 
  122.  
  123.    function openFileOpenDialog
  124.       local d
  125.       d = new FileOpenForm()
  126.       d.mdi = false
  127.       d.app = this
  128.    return ( d.readModal() )
  129.  
  130.    function openInvoiceOpenDialog( nCustomerID )
  131.       local d, bFound
  132.       bFound = false
  133.       d = new InvoiceOpenForm()
  134.       d.mdi  = false
  135.       d.app  = this
  136.       d.fillInvoiceList( nCustomerID ) 
  137.  
  138.       if ( d.iCount > 0 )
  139.          bFound := true
  140.          d.readModal()
  141.       else
  142.          MSGBOX( "No invoices found for customer number: " + ;
  143.                  LTRIM(STR(nCustomerID)), ;
  144.                  "Customer - " + LTRIM(STR(nCustomerID)), ;
  145.                  0)
  146.       endif
  147.    return ( bFound )
  148.  
  149.    function openCustomerSelectDialog( nID )
  150.       local d
  151.       d = new CustomerSelectForm()
  152.       d.selectID = nID
  153.       d.mdi := false
  154.       d.readModal()
  155.       nID := d.selectID
  156.    return ( nID )
  157.  
  158.  
  159.    function openToolbarDialog
  160.       local d
  161.       d = new ToolbarForm()
  162.       d.app = this
  163.       d.mdi := false
  164.    return ( d.readModal() )
  165.  
  166.    function openAboutDialog
  167.       local d
  168.       d = new AboutForm()
  169.       d.app = this
  170.       d.mdi := false
  171.    return ( d.readModal() )
  172.  
  173.    function openFinderDialog( thisForm )
  174.       local d
  175.       d = new FinderForm()
  176.       d.rowset := thisForm.rowset
  177.       d.mdi    := false
  178.    return ( d.readModal() )
  179.  
  180.    function openDataForm( f )
  181.       local m, bOpen
  182.       m = new mugsMenu( f, "Root" )
  183.       this.toolbars.databar.attach( f )   
  184.       bOpen = f.open()  
  185.       f.setFocus()
  186.    return ( bOpen )
  187.  
  188.    function openCustomerForm
  189.       local f, bOpen
  190.       f = new CustomerForm()
  191.       f.app = this
  192.       bOpen = f.app.openDataForm( f )
  193.    return ( bOpen )
  194.  
  195.    function openInvoiceForm( nInvoiceID )
  196.       local f, bOpen
  197.       f = new InvoiceForm()
  198.       f.app = this
  199.       f.visible := false
  200.       bOpen = f.app.openDataForm( f )
  201.       if ( PCOUNT() == 1 )
  202.          f.rowset.applyLocate( '"Invoice ID" = ' + STR(nInvoiceID) )
  203.       endif
  204.       f.visible := true
  205.    return ( bOpen )
  206.  
  207.    function openInventoryForm
  208.       local f, bOpen
  209.       f = new InventoryForm()
  210.       f.app = this
  211.       bOpen = f.app.openDataForm( f )
  212.    return ( bOpen )
  213.  
  214.    function openSupplierForm
  215.       local f, bOpen
  216.       f = new SupplierForm()
  217.       f.app = this
  218.       bOpen = f.app.openDataForm( f )
  219.    return (bOpen)
  220.  
  221.    function openSupplierFormAsDialog( nID )
  222.       local f, bRead
  223.       f = new SupplierForm()     
  224.       bFound = f.rowset.applyLocate('"Supplier ID" = ' + nID )
  225.       f.app = this
  226.       f.title.text := "<h3>" + f.rowset.fields["Company"].value + "</h3>"
  227.       f.text   := "Supplier " + nID
  228.       f.rowset := null  
  229.       f.mdi    := false
  230.       f.width  := 76
  231.       with ( f.book1 )
  232.          dataSource = "ARRAY {'Supplier'}"
  233.          width := 74
  234.          buttonOK.visible := true
  235.          buttonCancel.visible := true
  236.       endwith
  237.       f.form_onSize(0, f.width, f.height)
  238.       bRead  = f.ReadModal()
  239.    return ( bRead )
  240.  
  241.    function readIChart
  242.       local f
  243.       f = new IChartForm()
  244.       f.mdi := false
  245.    return ( f.readModal() )
  246.  
  247.    function renderInvoice
  248.       local bOK, nInvoice, f, r
  249.       bOK = false
  250.       nInvoice = 0
  251.       f = new IReportForm()
  252.       f.mdi := false
  253.       f.readModal()
  254.       bOK := f.OK
  255.       nInvoice := f.spinInvoice.value
  256.       if ( bOK )
  257.          r = new InvoiceReport()
  258.          r.invoice1.rowset.filter := '"Invoice ID" = ' + nInvoice
  259.          if ( r.invoice1.rowset.count() > 0 )
  260.             r.render()
  261.          else
  262.             MSGBOX("Invoice ID " + nInvoice + " not found.", ;
  263.                    "Alert", 0 )
  264.          endif
  265.       endif
  266.    return ( bOK )
  267.  
  268. endclass
  269.  
  270. class RestoreSet( sRunFolder )
  271.    this.folder    = sRunFolder
  272.    this.speedBar  = _app.speedBar 
  273.    this.frameText = _app.framewin.text
  274. endclass
  275.  
  276. class MugBars( app )
  277.    this.app = app
  278.    this.flat  = true
  279.    this.large = false
  280.    this.hints = true
  281.  
  282.    function create
  283.       this.appBar  = new MugAppbar( this.app )
  284.       this.dataBar = new MugDatabar( this.app )
  285.    return true
  286.  
  287.    function reset
  288.       this.appBar.reset()
  289.       this.dataBar.reset()     
  290.    return true
  291.  
  292.    function release
  293.       this.appBar  := null
  294.       this.dataBar := null
  295.    return true
  296.  
  297. endclass
  298.  
  299. class dataAction( app )
  300.    this.app    = app
  301.  
  302.    function goFirst( thisForm )
  303.       local bFirst
  304.       bFirst = false 
  305.       if ( class::rowActive( thisForm ) )         
  306.          bFirst := thisForm.rowset.first()
  307.       endif
  308.       this.refreshMenu( thisForm )
  309.    return ( bFirst )
  310.  
  311.    function goPrev( thisForm )
  312.       local bPrev
  313.       bPrev = false
  314.       if ( class::rowActive( thisForm ) )
  315.          if ( not thisForm.rowset.atFirst() ) 
  316.             bPrev := thisForm.rowset.next( -1 )
  317.             if ( not bPrev ) 
  318.                thisForm.rowset.next() 
  319.             endif
  320.          endif
  321.       endif
  322.       this.refreshMenu( thisForm )
  323.    return ( bPrev )
  324.  
  325.    function goNext( thisForm )
  326.       local bNext
  327.       bNext = false
  328.       if ( class::rowActive( thisForm ) )
  329.          if ( not thisForm.rowset.atLast() )
  330.             bNext := thisForm.rowset.next()
  331.             if ( not bNext  )
  332.                thisForm.rowset.next(-1)
  333.             endif
  334.          endif
  335.       endif
  336.       this.refreshMenu( thisForm )
  337.    return ( bNext )
  338.  
  339.    function goLast( thisForm )
  340.       local bLast
  341.       bLast = false
  342.       if ( class::rowActive( thisForm ) )
  343.          bLast := thisForm.rowset.last()
  344.       endif
  345.       this.refreshMenu( thisForm )
  346.    return ( bLast )
  347.  
  348.    function locateRow( thisForm ) 
  349.       local bFind
  350.       bFind = false
  351.       if ( class::rowActive (thisForm ) )
  352.          bFind := thisForm.app.openFinderDialog( thisForm )
  353.       endif
  354.    return ( bFind )
  355.  
  356.    function appendRow( thisForm )
  357.       local bAppend
  358.       bAppend = false
  359.       if ( class::rowActive( thisForm ) )
  360.          try
  361.             bAppend := thisForm.rowset.beginAppend()
  362.          catch ( Exception e )
  363.             MSGBOX("Unable to add a new row.","Alert",48)
  364.             bAppend := false
  365.             thisForm.rowset.abandon()
  366.          endtry
  367.       endif
  368.       this.refreshMenu( thisForm )
  369.    return ( bAppend )
  370.  
  371.    function deleteRow( thisForm )
  372.       local bDelete 
  373.       bDelete = false
  374.       if ( class::rowActive( thisForm ) )
  375.          if ( MSGBOX("You are about to delete the current row." ;
  376.                 + CHR(13) ;
  377.                 + "Click Yes to delete the current row.", ;
  378.                   "Confirm", ;
  379.                   4) == 6 )
  380.             try 
  381.                bDelete := thisForm.rowset.delete()
  382.             catch ( Exception e )
  383.                MSGBOX("Unable to delete current row.","Alert",48)
  384.                bDelete := false
  385.             endtry
  386.             this.goPrev( thisForm )
  387.          endif
  388.       endif
  389.       this.refreshMenu( thisForm )
  390.    return ( bDelete )
  391.  
  392.    function saveRow( thisForm )
  393.       local bSave
  394.       bSave = false
  395.       if ( class::rowActive( thisForm ) )
  396.          bSave := thisForm.rowset.save()
  397.       endif
  398.    return ( bSave )
  399.  
  400.    
  401.    function refreshRow( thisForm )
  402.       local bRefresh
  403.       bRefresh = false
  404.       if ( class::rowActive( thisForm ) )
  405.          bRefresh := thisForm.rowset.refreshControls()
  406.       endif
  407.    return ( bRefresh )
  408.  
  409.  
  410.    function abandonRow( thisForm )
  411.       local bAbandon
  412.       bAbandon = false
  413.       if ( class::rowActive( thisForm ) )
  414.          bAbandon := thisForm.rowset.abandon()
  415.       endif
  416.       this.refreshMenu( thisForm )
  417.    return ( bAbandon )
  418.  
  419.    function rowActive( thisForm )
  420.       local bActive
  421.       bActive = true
  422.       if ( thisForm.rowset == null ) OR ;
  423.          ( NOT thisForm.rowset.parent.active ) 
  424.          bActive := false      
  425.       endif
  426.    return ( bActive )
  427.  
  428.    function refreshMenu( thisForm )
  429.        local bAtFirst, bAtLast
  430.        bAtFirst = thisForm.rowset.atFirst()
  431.        bAtLast  = thisForm.rowset.atLast()
  432.  
  433.        with ( thisForm.root.menuTable )
  434.           menuFirst.enabled := ( not bAtFirst )
  435.           menuPrev.enabled  := ( not bAtFirst )
  436.           menuNext.enabled  := ( not bAtLast )
  437.           menuLast.enabled  := ( not bAtLast )
  438.        endwith
  439.    return ( ( not bAtFirst ) and ( not bAtLast ) )
  440.  
  441. endclass
  442.  
  443.